package com.ftg.learn.mapper;

import com.ftg.learn.entity.Student;
import com.sun.jmx.remote.security.JMXSubjectDomainCombiner;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.executor.CachingExecutor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kangjx
 * @createTime 2022/11/23
 */
public class StudentDao {
//
//    static class Student{
//        int cno;
//        String cname;
//        String pass;
//
//        public int getCno() {
//            return cno;
//        }
//
//        public void setCno(int cno) {
//            this.cno = cno;
//        }
//
//        public String getCname() {
//            return cname;
//        }
//
//        public void setCname(String cname) {
//            this.cname = cname;
//        }
//
//        public String getPass() {
//            return pass;
//        }
//
//        public void setPass(String pass) {
//            this.pass = pass;
//        }
//    }
//
//
//    public static void main(String[] args) {
//
////        jdom
////                dom4j
//
//        try (
//                FileInputStream fis = new FileInputStream(new File("/home/kangjx/Desktop/user.txt"));
//            ){
//            String content = "";
//            int num = 0;
//            while((num = fis.read()) != -1){
//                System.out.println(num);
//                content += ((char)num)+"";
////                if(num == 45){
////                    content = "";
////                }
//            }
//            String[] contents = content.split("  ");
//            Student stu = new Student();
//            stu.setCno(Integer.valueOf(contents[0]));
//            stu.setCname(contents[1]);
//            stu.setPass(contents[2]);
//
//            System.out.println(stu.getCno() + " txt里面的id");
//            System.out.println(stu.getCname() + " name");
//            System.out.println(stu.getPass() + " pass");
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


    public static void main(String[] args) {
        StudentDao s = new StudentDao();
        System.out.println(s.showall());
    }

    public List<Map<String,Object>> showall(){
        String path = "mybatisconfig.xml";
        List<Map<String,Object>> list = null;
        try {
            //reader流  里面全是配置文件信息
            Reader reader = Resources.getResourceAsReader(path);
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);

            ///上面的代码只是完成了对mybatis核心配置类里面的参数转换功能
//SqlSessionFactory 实现类是 DefaultSqlSessionFactory
            Configuration configuration = sqlSessionFactory.getConfiguration();
            //configuration 就是你从xml里解析出来的所有参数存储的java类

//            SqlSession  采是应用层方法 封装的添加修改删除等操作
            SqlSession session = sqlSessionFactory.openSession();
            //openSessionFromDataSource
            list = session.selectList("student.sel");
            Map<String,Object> m = new HashMap<>();
            m.put("sno","s001");
            m.put("sname","张三");
            System.out.println( "student.select3==>"+session.selectList("student.select3",m));
            System.out.println(list);


            Student s = new Student();
            s.setSno("s001");
            s.setSname("张三");
            List<Student> objects = session.selectList("student.select4", s);
            System.out.println("select4==>"+objects);


            Student s1 = new Student();
            s1.setSname("李四");
            List<Student> stus = session.selectList("student.select5", s1);


            stus.forEach(x ->{
                System.out.println("是select5" + x.getSname());
            });

            Student s2 = new Student();
            s2.setSname("李四");
            List<Student> stu6 = session.selectList("student.select6", s2);

            System.out.println("select 6");
            stu6.forEach(x->{
                System.out.println("是select6" + x.getSname());
            });

            List<String> pars = Arrays.asList("s001","s002","s003");
            List<Student> stu7 = session.selectList("student.select7", pars);

            System.out.println("select 7");
            stu7.forEach(x->{
                System.out.println("是select7" + x.getSname());
            });

            //可以执行sql语句的地方  上面sqlSessionFactoryBuilder.build(reader); 返回的信息 config
//            传递给了DefaultSqlSessionFactory (config)
//            public SqlSessionFactory build(Configuration config) {
//                return new DefaultSqlSessionFactory(config);
//            }


//            mybatis 核心配置类叫什么 Configuration  ?

//            哪一个类完成的解析操作  xmlconfigbuilder类里面的 parse 返回了 Configuration

//            public Configuration parse() {
//                if (parsed) {
//                    throw new BuilderException("Each XMLConfigBuilder can only be used once.");
//                }
//                parsed = true;
//                //evalNode 节点转换 configuration 这是根节点呀
//                parseConfiguration(parser.evalNode("/configuration"));
//                return configuration;
//            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        //jdbc  mybatis

        //sql mybatis MappedStatement

        //执行一下 mybatis -- excutor - mapperstatement里面的sql
        return list;
    }

}
